<?php

/**
* CodeListModule.class.php
*
* Module responsable for code hunt
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once("Auth/Auth.php");
require_once("framework/config.php");
require_once("UserAuth.php");
require_once("framework/PageModule.class.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("HTML/Template/IT.php");
require_once("OverviewMapModule.class.php");


// {{{ AuthForm

class CodeListModule extends PageModule 
{
    
    
    private $dbh = null;

    function CodeListModule()
    {

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $this->dbh = new PDO($options['dsn_string']);
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    }

    // {{{ getContent()

    
    /**
    * getContent()
    *
    */

    function getContent()
    {

        trigger_error("ENTER CodeListModule::getContent()", E_USER_NOTICE);

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES');        
        $tpl = new HTML_Template_IT($options["templates_dir"]);
        $tpl->loadTemplatefile("code_list.tpl.xhtml",true,true);

        $statusTpl = new HTML_Template_IT($options["templates_dir"]);
        $statusTpl->loadTemplatefile("status.tpl.xhtml",FALSE,FALSE);

        $sth = $this->dbh->prepare('SELECT guide.rowid AS rowid,(SELECT name FROM guide_name WHERE guide_name.locale = :locale AND guide_id = guide.rowid) AS name FROM guide WHERE publish = 1 ORDER BY name ASC');
        $sth -> bindParam(':locale',  LocalizationSingleton::getLocale(), PDO::PARAM_STR);
        $sth->execute();
        $tpl->setVariable("LEAD","");
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);

        $indexArray = array();
        foreach (range(1, 9) as $number) {
           $indexArray[] = $number;
        }
        foreach (range('A', 'Z') as $letter) {
           $indexArray[] = $letter;
        }


        if ( count($result) )
        {
            foreach($result as $key => $value)
            {

                $sth = $this->dbh->prepare('SELECT COUNT(rowid) as count FROM hunted_code WHERE code_id = :id AND owner = :owner');
                $sth->bindValue(':owner', AuthSingleton::instance('AUTH_USER')->getUsername(),PDO::PARAM_STR);
                $sth->bindValue(':id', $value['rowid'],PDO::PARAM_INT);
                $sth->execute();    
                $row = $sth->fetch(PDO::FETCH_ASSOC);

                if( $row['count'] )
                {
                    $tpl->setCurrentBlock('hunted_row');
                    
                }
                else
                {
                    $tpl->setCurrentBlock('not_hunted_row');
                }

                $index = $key%count($indexArray);
                $tpl->setVariable("NUM",$indexArray[$index]);
                $tpl->setVariable("URL",rtrim($global['service_url'],'/').'/?cmd=ShowCode&id='.$value['rowid']);
                $tpl->setVariable("TITLE",$value['name'] == '' ? sprintf(_('Brak tłumaczenia dla %s'),LocalizationSingleton::getLocale()) : $value['name']);

                if( $row['count'] )
                {
                    $tpl->parseCurrentBlock('hunted_row');                       
                }
                else
                {
                    $tpl->parseCurrentBlock('not_hunted_row');       
                }
                
                $tpl->parse("row");                                                  
            }

        }
        else
        {

            $tpl->setCurrentBlock('empty_row');
            $tpl->setVariable("INFO",_('Brak kodów'));
            $tpl->parseCurrentBlock('empty_row');                              
        }
        
        $statusTpl->setVariable("STATUS",_('Mapa poglądowa oraz lista trofeów'));
        $overviewMap =  new OverviewMapModule();

        return $statusTpl->get().$overviewMap->getContent().$tpl->get().$statusTpl->get();
    }
    
    // }}}
    

}

// }}} 

?>

